package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.service.UserLikePostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class UserLikePostController {
    @Autowired
    private UserLikePostService userLikePostService;

    @GetMapping("/isLike")
    public String isLike (int userId, int postId) {
        JSONObject res = new JSONObject();
        if (userLikePostService.isLike(userId, postId) != null) {
            res.put("code", 200);
            res.put("message", "已经点赞");
            res.put("isLike", true);
        } else {
            res.put("code", 200);
            res.put("message", "没有点赞");
            res.put("isLike", false);
        }
        return res.toJSONString();
    }

    // 改变关注
    @GetMapping("/changeLike")
    public String changeLike (boolean isLike, int userId, int postId) {
        JSONObject res = new JSONObject();
        if (userLikePostService.changeLike(isLike, userId, postId) == 1) {
            res.put("code", 200);
            res.put("message", "更改成功");
        } else {
            res.put("code", 403);
            res.put("message", "更改失败");
        }
        return res.toJSONString();
    }
}
